package com.example.experiment4;

import android.opengl.GLSurfaceView;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GLSurfaceView glView = new GLSurfaceView(this);
        MyRender render = new MyRender();
        //为GLSurfaceView设置绘制器
        glView.setRenderer(render);
        setContentView(glView);
    }

    class MyRender implements GLSurfaceView.Renderer {
        private final float[] vertexes = new float[]{
                //上顶面四个点
                0.5f, 0.5f, 0.5f,
                0.5f, -0.5f, 0.5f,
                -0.5f, -0.5f, 0.5f,
                -0.5f, 0.5f, 0.5f,
                //下底面四个点
                0.5f, 0.5f, -0.5f,
                0.5f, -0.5f, -0.5f,
                -0.5f, -0.5f, -0.5f,
                -0.5f, 0.5f, -0.5f
        };
        //六个面
        private final byte[] faces = new byte[]{
                0, 1, 2,
                0, 2, 3,
                2, 3, 7,
                2, 6, 7,
                0, 3, 7,
                0, 4, 7,
                4, 5, 6,
                4, 6, 7,
                0, 1, 4,
                1, 4, 5,
                1, 2, 6,
                1, 5, 6,
        };
        //buffer对象
        FloatBuffer vertexFloatBuffer;
        ByteBuffer faceByteBuffer;
        //旋转的角度
        float rotate;

        public MyRender() {
            vertexFloatBuffer = floatBufferUtil(vertexes);
            faceByteBuffer = ByteBuffer.wrap(faces);
        }

        @Override
        public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
            //关闭抗抖动
            gl10.glDisable(GL10.GL_DITHER);
            //透视修正
            gl10.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
            gl10.glClearColor(0, 0, 0, 0);
            //阴影平滑模式
            gl10.glShadeModel(GL10.GL_SMOOTH);
            //启用深度测试
            gl10.glEnable(GL10.GL_DEPTH_TEST);
            //深度测试的类型
            gl10.glDepthFunc(GL10.GL_LEQUAL);
        }

        @Override
        public void onSurfaceChanged(GL10 gl10, int i, int i1) {
            //设置3D视窗的大小及位置
            gl10.glViewport(0, 0, i, i1);
            //将当前矩阵模式设为投影矩阵
            gl10.glMatrixMode(GL10.GL_PROJECTION);
            //初始化单位矩阵
            gl10.glLoadIdentity();
            //设置宽度高度比
            float ratio = (float) i / i1;
            //调用此方法设置透视视窗的空间大小
            gl10.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
        }

        //绘制图形的方法
        @Override
        public void onDrawFrame(GL10 gl10) {
            //清除屏幕缓存和深度缓存
            gl10.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
            //启用顶点坐标数据
            gl10.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            //启用顶点颜色数据
            gl10.glEnableClientState(GL10.GL_COLOR_ARRAY);
            //设置当前矩阵模型为模型视图
            gl10.glMatrixMode(GL10.GL_MODELVIEW);
            //开始绘制
            //重置当前模型视图矩阵
            gl10.glLoadIdentity();
            gl10.glTranslatef(0.7f, 0.0f, -2.2f);
            //沿着Y轴旋转
            gl10.glRotatef(rotate, 0f, 0.2f, 0f);
            //沿着X轴旋转
            gl10.glRotatef(rotate, 1f, 0f, 0f);
            //设置顶点的位置数据
            gl10.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexFloatBuffer);
            //不设置顶点的颜色数据，还用以前的颜色数据
            //按照vertexFloatBuffer指定的面绘制三角形
            gl10.glDrawElements(GL10.GL_TRIANGLE_STRIP, faceByteBuffer.remaining(), GL10.GL_UNSIGNED_BYTE, faceByteBuffer);
            //绘制结束
            gl10.glFinish();
            gl10.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            //旋转角度加1
            rotate += 1;
        }

        private FloatBuffer floatBufferUtil(float[] arr) {
            FloatBuffer mBuffer;
            ByteBuffer qbb = ByteBuffer.allocateDirect(arr.length * 4);
            //数组排列用nativeOrder
            qbb.order(ByteOrder.nativeOrder());
            mBuffer = qbb.asFloatBuffer();
            mBuffer.put(arr);
            mBuffer.position(0);
            return mBuffer;
        }
    }
}